//
//  LZWelcomeController.swift
//  Weibo
//
//  Created by lzing on 16/3/24.
//  Copyright © 2016年 lzing. All rights reserved.
//

import UIKit
import SnapKit

class LZWelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        /// 布局子控件
        prepareUI()
        ///  加载头像数据
        loadIconMessage()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        ///  开启动画
        startAnimation()
    }

    /// 布局子控件
    private func prepareUI() {
        // 添加子控件
        addSubview()
        // 添加约束
        addConstraint()
    }
    
    ///  加载头像数据
    private func loadIconMessage() {
        // 先使用本地图片
        if let iconString = LZUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large {
            //  加载本地
            let url = NSURL(string: iconString)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        // 再加载网络图片
        LZUserAccountViewModel.sharedUserAccountViewModel.loadUserInfo { (error) -> () in
            if error != nil {
                LZPrint(msg: "加载错误")
                return
            }
            if let iconString = LZUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large {
                //  加载本地
                let url = NSURL(string: iconString)
                self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }
    }
    
    ///  开启动画
    private func startAnimation() {
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(LZDefaultDuration, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 动画完成
                        AppDelegate.outSwitchRootViewController(LZMainController())
                })
        }
    }
    
    // 添加子控件
    private func addSubview() {
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
    }
    
    // 添加约束
    private func addConstraint() {
        // 给背景添加约束
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        // 给头像添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 85, height: 85))
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
        }
        // 给label添加约束
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
    // MARK: - lazy
    // 背景
    private lazy var bkgImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    // 头像
    private lazy var iconView : UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        // 设置圆角
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
        return iconView
    }()
    
    // 欢迎label
    private lazy var welcomeLabel : UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(14)
        label.sizeToFit()
        label.alpha = 0
        return label
    }()
}
